﻿import ajaxOptions = require("AjaxCallsOptions");
import config = require("Configuration");
import dataAccess = require("Data-access");
import models = require("Models");
import common = require("Common");
import application = require("WindowExtensions");

export class UserAccount extends kendo.data.ObservableObject {

    constructor() {
        super();
        super.init(this);
    }

    isUserLoggedIn: boolean = false;
    firstName: string = "";
    lastName: string = "";
    userName: string = "username";
    password: string = "password";
    userAddress: string = "";
    userEmailAddress: string = "";
    subscribedForNewsLetter: boolean = false;
    userBookingHistory = [];

    private _instance = this;

    userLogin = () => {
        var loginOptions = new ajaxOptions.AjaxOptions();
        loginOptions.url = config.Configuration.accoutUrl;
        loginOptions.requestType = "GET";
        loginOptions.dataType = "JSON";
        loginOptions.httpHeader = "Authorization";
        // btoa function will convert the text to base64 encoding
        loginOptions.headerValue = "Basic " + btoa(this.userName + ":" + this.password);

        dataAccess.callService(loginOptions, this._instance, this.fnLoginCallBack);
    };

    fnLoginCallBack(model: kendo.data.ObservableObject, result: ajaxOptions.AjaxOptionsCallbackInput) {
        if (result.success !== true) {
            alert("Error generated in fnLoginCallBack");
        } else {
            var userModel = <models.UserModelServerSide>result.data;
            if (userModel === undefined || userModel === null) {
                alert("Incorrect return type");
                return;
            }

            model.set("firstName", userModel.FirstName);
            model.set("lastName", userModel.LastName);
            model.set("userAddress", userModel.Address);
            model.set("userEmailAddress", userModel.EmailId);
            model.set("userBookingHistory", userModel.BookingHistory);
            model.set("isUserLoggedIn", true);
            model.set("subscribedForNewsLetter", userModel.SubscribedForNewsLetter);

            common.showLogOfButton();
        }
    }

    updateUserDetails() {
        var updateOptions = new ajaxOptions.AjaxOptions();
        updateOptions.url = config.Configuration.accoutUrl;
        updateOptions.requestType = "POST";
        updateOptions.dataType = "JSON";
        updateOptions.httpHeader = "Authorization";
        // btoa function will convert the text to base64 encoding
        updateOptions.headerValue = "Basic " + btoa(this.userName + ":" + this.password);

        var data = new models.UserModelServerSide();
        data.FirstName = this.firstName;
        data.LastName = this.lastName;
        data.Address = this.userAddress;
        data.EmailId = this.userEmailAddress;
        data.SubscribedForNewsLetter = this.subscribedForNewsLetter;

        updateOptions.data = data;

        dataAccess.callService(updateOptions, this._instance, () => { alert("Details updated"); });
    }

    logOff() {
        console.log("inside logoff");
        this.firstName = "";
        this.lastName = "";
        this.userAddress = "";
        this.userEmailAddress = "";
        this.userBookingHistory = [];
        this.isUserLoggedIn = false;

        common.showLogOfButton();
        common.navigateToView("UserAccount.html");
    }
}

var appInit = application.getAppInitProperty();
appInit.accountModelInit = () => {
    var viewModel = new UserAccount();
    kendo.bind($("#mt-theaters-view"), viewModel);
}

